package com.chookss.tiku

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import com.chookss.R
import com.chookss.tiku.adapter.LeftListAdapter
import com.chookss.tiku.adapter.RightPracticeListAdapter
import com.chookss.tiku.entity.LeftEntiry
import com.chookss.tiku.entity.MyExam
import com.chookss.tools.old.SignUtils
import com.chookss.view.LianxiDialog
import com.johnrambo.ktea.ktExtends.getViewModel
import com.johnrambo.ktea.ktExtends.observe
import com.lvgroup.hospital.base.BaseAct
import infoToast
import kotlinx.android.synthetic.main.activity_select_practice_item.*
import kotlinx.android.synthetic.main.common_title.*
import java.util.*

/**专项练习 一级选择页面
 */
@RequiresApi(Build.VERSION_CODES.N)
class SelectPracticeCatalog : BaseAct(), View.OnClickListener{

    private var parentCode: String = "0001"
    //private var examCode:String =""
    private var catalogCodes:String = ""
    private var replaceCatalogCodes:String = ""



    private val model by lazy { getViewModel<SelectPracticeModel>() }

    //private val leftAdapter by lazy { LeftListAdapter() }
    private val leftAdapter by lazy {
        LeftListAdapter().apply {
            listener = { item ->
                tvPracticeChangyong.setBackgroundColor(Color.parseColor("#F7F7F7"))
                tvPracticeChangyong.isSelected=false
                parentCode = item.catalogCode
                initRight(item.childList)
            }
        }
    }
    private val rightAdapter by lazy {
        RightPracticeListAdapter().apply {
            listener = { catCodes ->
                catalogCodes = catCodes
                val replaceStr = catalogCodes.replace("[", "", false).replace("]","",false)
                //str.replaceAll("\\s*", "")
                replaceCatalogCodes = replaceStr.replace("\\s".toRegex(), "")
                Log.i("catalogCodes","catalogCodes:$replaceCatalogCodes")
            }
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_select_practice_item)
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun initData() {
        initState()
        common_title_txt.setText("专项练习")
        common_right_iv_ll.visibility=View.VISIBLE
        common_right_iv.setImageResource(R.drawable.wenhao_45)
        tvPracticeChangyong.setBackgroundColor(Color.parseColor("#ffffff"))
        tvPracticeChangyong.isSelected=true
        common_title_back.setOnClickListener(this)
        common_right_iv_ll.setOnClickListener(this)
        btnSimulation.setOnClickListener(this)
        tvPracticeChangyong.setOnClickListener(this)
        rcPracticeLeft.layoutManager = LinearLayoutManager(this@SelectPracticeCatalog);
        rcPracticeLeft.adapter = leftAdapter
        rcPracticeRight.layoutManager = LinearLayoutManager(this@SelectPracticeCatalog);
        rcPracticeRight.adapter = rightAdapter

    }

    override fun onStart() {
        super.onStart()
        initLeft()
        initCyRight()
    }
    @RequiresApi(Build.VERSION_CODES.N)
    private fun initLeft() {

        val params1: MutableMap<String, Any> = HashMap()
        params1["catalogType"] = "1"; //目录类型,1专项练习 2 模拟卷
        val timeStamp1 = SignUtils.getTimeStamp() //20201208125238
        val randomString1 = SignUtils.getRandomString(14)
        val makeSignStr1 = SignUtils.makeSignStr(params1, timeStamp1, randomString1, false)

        model.getLeft("1", timeStamp1, randomString1, makeSignStr1)
        model.leftLiveData.observe(this) {
            leftAdapter.update(it)
        }
    }


    @RequiresApi(Build.VERSION_CODES.N)
    private fun initRight(childList:List<LeftEntiry>) {
        rcPracticeRight.layoutManager = LinearLayoutManager(this@SelectPracticeCatalog);
        rcPracticeRight.adapter = rightAdapter

        catalogCodes = ""
        replaceCatalogCodes = ""

        rightAdapter.update(tolvl2List(childList))

//        val params2: MutableMap<String, Any> = HashMap()
//        params2["catalogType"] = "1"
//        params2["parentCode"] = parentCode;
//        val timeStamp2 = SignUtils.getTimeStamp() //20201208125238
//        val randomString2 = SignUtils.getRandomString(14)
//        val makeSignStr2 = SignUtils.makeSignStr(params2, timeStamp2, randomString2, false)
//
//        model.getRight("1",parentCode, timeStamp2, randomString2, makeSignStr2)
//        model.rightLiveData.observe(this) {
//            rightAdapter.update(it)
//        }
    }

    @RequiresApi(Build.VERSION_CODES.N)
    private fun initCyRight() {

        val params3: MutableMap<String, Any> = HashMap()
        val timeStamp3 = SignUtils.getTimeStamp() //20201208125238
        val randomString3 = SignUtils.getRandomString(14)
        val makeSignStr3 = SignUtils.makeSignStr(params3, timeStamp3, randomString3, false)

        model.getCyRight(timeStamp3, randomString3, makeSignStr3)
        model.rightCyLiveData.observe(this) {
            rightAdapter.update(tolvl2List(it))
        }
    }

    fun tolvl2List(childList:List<LeftEntiry>):List<LeftEntiry> {
        var list = java.util.ArrayList<LeftEntiry>()
        childList.forEach {
            if (it.childList == null || it.childList.isEmpty()) {
                list.add(it.apply { lvlNo = 2 })
            } else {
                list.add(it.apply { lvlNo = 1 })
                it.childList?.forEach {
                    list.add(it.apply { lvlNo = 2 })
                }
            }
        }
        return list
    }


    override fun onClick(v: View?) {
        when(v?.id){
            R.id.common_title_back->{
                finish()
            }
            R.id.common_right_iv_ll->{
                LianxiDialog(this, "目录熟练度根据记忆曲线掌握情况而定，计算规则为“已牢记题目数量÷目录下的题目总数”").show()

            }
            //列表最上面的 常用
            R.id.tvPracticeChangyong->{
                tvPracticeChangyong.setBackgroundColor(Color.parseColor("#ffffff"))
                initCyRight()
                initLeft()
            }
            //模拟卷 开始做题Button
            R.id.btnSimulation->{
                if(replaceCatalogCodes.isEmpty()){
                    infoToast("请选择一项练习内容")
                }else{
                    var intent = Intent(this, PracticeTestActivity::class.java)
                    intent.putExtra("practiceType", "2") //练习类型 1智能题库 2 专项练习
                    intent.putExtra("catalogCodes", replaceCatalogCodes) //目录编码，多个用逗号分隔(专项练习模式下此参数必传)
                    startActivity(intent)
                    replaceCatalogCodes=""
                }

            }
        }
    }

    fun gotoSimulateActivity(exam: MyExam) {
        var intent = Intent(this, AnswerExamActivity::class.java)
        intent.putExtra("exam", exam)
        intent.putExtra("answerType", "2")
        startActivity(intent)
    }



}